/**
 * Created by forest on 10/25/14.
 */
/**
 * Created by Forest on 12/30/13.
 */

if (!zx_ns_common) {
    var zx_ns_common = (function () {

        //read COOKIE
        var getCookie = function (cookiename) {
            var result;
            var mycookie = document.cookie;
            var start2 = mycookie.indexOf(cookiename + "=");
            if (start2 > -1) {
                start = mycookie.indexOf("=", start2) + 1;
                var end = mycookie.indexOf(";", start);

                if (end == -1) {
                    end = mycookie.length;
                }
                result = unescape(mycookie.substring(start, end));
            }
            return result;
        };

        //write COOKIE
        var setCookie = function (cookiename, cookievalue, hours) {
            var date = new Date();
            date.setTime(date.getTime() + Number(hours) * 3600 * 1000);
            document.cookie = cookiename + "=" + cookievalue + "; path=/;expires = " + date.toGMTString();
        };

        var addCommas = function (nStr) {
            nStr += '';
            nStr = stripNonNumeric(nStr);
            var x = nStr.split('.');
            var x1 = x[0];
            var x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        };

        var formatNumber = function (pnumber, decimals) {
            var num = stripNonNumeric("" + pnumber + "");
            if (isNaN(pnumber) || pnumber == '') {
                num = 0;
            }
            return parseFloat(num).toFixed(decimals);
        };

        var stripNonNumeric = function (str) {
            str += '';
            var rgx = /^d|.|-$/;
            var out = '';
            for (var i = 0; i < str.length; i++) {
                if (rgx.test(str.charAt(i))) {
                    if (!( ( str.charAt(i) == '.' && out.indexOf('.') != -1 ) ||
                            ( str.charAt(i) == '-' && out.length != 0 ) )) {
                        out += str.charAt(i);
                    }
                }
            }
            return out;
        };

        // Element Attribute Helper
        var attrZXValue = function ($el, data_var, default_val) {
            if (typeof $el.attr(data_var) != 'undefined') {
                return $el.attr(data_var);
            }
            return default_val;
        };


        // Element Attribute Helper
        var attrZXValue2 = function ($el, data_var, default_val) {
            if (typeof $el.data(data_var) != 'undefined') {
                return $el.data(data_var);
            }
            return default_val;
        };

        var getUrlVars = function (url) {
            var vars = new Object(), hash;
            if (url.indexOf("?") >= 0) {
                var hashes = url.slice(url.indexOf('?') + 1).split('&');
                for (var i = 0; i < hashes.length; i++) {
                    hash = hashes[i].split('=');
                    vars[hash[0]] = hash[1];
                }
            }
            return vars;
        };

        var getUrlVar = function (name, url) {
            if (url == undefined || !url) {
                url = window.location.href
            }
            return getUrlVars(url)[name];
        };


        // function getFileSize(target) {
        //     var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
        //     var fileSize = 0;
        //     if (isIE && !target.files) {
        //         var filePath = target.value;
        //         var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
        //         var file = fileSystem.GetFile(filePath);
        //         fileSize = file.Size;
        //     } else {
        //         fileSize = target.files[0].size;
        //     }
        //     return fileSize;
        // }

        // not working do not use this method
        var isIOS = function () {
            var ua = navigator.userAgent;
            return /iPhone(\d+\.\d+)/.test(ua);
        };

        var isAndroid = function () {
            var ua = navigator.userAgent;
            return /Android (\d+\.\d+)/.test(ua);
        };

        var isInWechatBrowser = function () {
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                return true;
            } else {
                return false;
            }
        };


        var getFileName = function (file) {//通过第一种方式获取文件名
            return file.name;
        };

        var getFileExtension = function (file) {
            var fileName = getFileName(file);
            var idx = fileName.lastIndexOf(".");
            if (idx >= 0) {
                return fileName.substring(idx);
            } else {
                return "";
            }
        };

        var getFileSize = function (file) {
            return file.size;
        };

        return {
            attrZXValue: attrZXValue,
            attrZXValue2: attrZXValue2,
            stripNonNumeric: stripNonNumeric,
            formatNumber: formatNumber,
            addCommas: addCommas,
            getUrlVars: getUrlVars,
            getUrlVar: getUrlVar,
            getFileSize: getFileSize,
            isIOS: isIOS,
            isAndroid: isAndroid,
            isInWechatBrowser: isInWechatBrowser,
            getCookie: getCookie,
            setCookie: setCookie,
            getFileName: getFileName,
            getFileExtension: getFileExtension
        }
    })();
}